unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, rtcInfo, rtcConn, rtcDataCli, rtcHttpCli,Dialog_View,
  rtcDataSrv, rtcHttpSrv, RzButton, RzRadChk,EncdDecd;

type
  TForm1 = class(TDialogView)
    HttpClient1: TRtcHttpClient;
    Btnx_1: TButton;
    Btnx_2: TButton;
    Btnx_3: TButton;
    DataRequest1: TRtcDataRequest;
    Btnx_4: TButton;
    HttpServer1: TRtcHttpServer;
    RtcDataProvider1: TRtcDataProvider;
    Chkb_1: TRzCheckBox;
    procedure Btnx_1Click(Sender: TObject);
    procedure DataRequest1DataReceived(Sender: TRtcConnection);
    procedure DataRequest1BeginRequest(Sender: TRtcConnection);
    procedure Btnx_3Click(Sender: TObject);
    procedure Btnx_4Click(Sender: TObject);
    procedure HttpClient1ConnectError(Sender: TRtcConnection;
      E: Exception);
    procedure HttpClient1ConnectFail(Sender: TRtcConnection);
    procedure Chkb_1Click(Sender: TObject);
    procedure RtcDataProvider1CheckRequest(Sender: TRtcConnection);
    procedure RtcDataProvider1DataReceived(Sender: TRtcConnection);
  private
  protected
    procedure SetInitialize;override;
    procedure SetCommParams;override;
    procedure SetGridParams;override;
    procedure SetComboItems;override;
    procedure TryFreeAndNil;override;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils;

{$R *.dfm}

{ TForm1 }

procedure TForm1.SetComboItems;
begin
  inherited;

end;

procedure TForm1.SetCommParams;
begin
  inherited;

end;

procedure TForm1.SetGridParams;
begin
  inherited;

end;

procedure TForm1.SetInitialize;
begin
  inherited;
  HttpClient1.ServerAddr:='192.168.0.21';
  HttpClient1.ServerPort:='8187';
  HttpClient1.Connect();
end;

procedure TForm1.TryFreeAndNil;
begin
  inherited;

end;

procedure TForm1.Btnx_1Click(Sender: TObject);
begin
  {DataRequest1.Request.Method:='POST';                                              // Use the "HTTP POST" method
  DataRequest1.Request.FileName:=Format('/%S/synchronizer!uploadCards.action?unit=%S',[ServerName,Context.GetUnitMatch]); // Set the "FileName" to receive the request
  DataRequest1.Request.Host:=HttpClient1.ServerAddr;                                  // Set the "Host" HTTP header
  DataRequest1.Request.ContentType:='application/x-www-form-urlencoded';
  DataRequest1.Request.Params.Value['data']:=URL_Encode(Value);
  DataRequest1.Post();}

  HttpClient1.ServerAddr:='192.168.0.51';
  HttpClient1.ServerPort:='8187';
  HttpClient1.Connect();

  DataRequest1.Request.Method  :='POST';                                              // Use the "HTTP POST" method
  DataRequest1.Request.FileName:='/TEST';
  DataRequest1.Request.Host:=HttpClient1.ServerAddr;
  DataRequest1.Request.ContentType:='application/x-www-form-urlencoded';
  DataRequest1.Post();
end;

procedure TForm1.DataRequest1DataReceived(Sender: TRtcConnection);
var
  DataClient:TRtcDataClient absolute  Sender;
begin
  with DataClient do
  begin
    if Response.Done then
    begin
      ShowMessage(DataClient.Read);
    end;
  end;
end;

procedure TForm1.DataRequest1BeginRequest(Sender: TRtcConnection);
var
  DataClient:TRtcDataClient absolute  Sender;
begin
  with DataClient do
  begin
    if Request.FileName='/GETGKZF' then
    begin
      DataClient.Write(Request.Params.Text);
    end else
    if Request.FileName='/GETTEMP' then
    begin
      DataClient.Write(Request.Params.Text);
    end else
    if Request.FileName='/TEST' then 
    begin
      DataClient.WriteHeader();
    end else
    if Request.FileName='/TUSER.INSERTDB' then
    begin
      DataClient.Write(Request.Params.Text);
    end else
    begin
      DataClient.WriteHeader();
    end;
  end;
end;

procedure TForm1.Btnx_3Click(Sender: TObject);
begin
  HttpClient1.ServerAddr:='192.168.0.51';
  HttpClient1.ServerPort:='8189';
  if HttpClient1.isConnecting then
  begin
    HttpClient1.Disconnect
  end;
  HttpClient1.Connect();

  DataRequest1.Request.Method  :='POST';
  DataRequest1.Request.FileName:=Format('/GETGKZF',[]);
  DataRequest1.Request.Host:=HttpClient1.ServerAddr;
  DataRequest1.Request.ContentType:='application/x-www-form-urlencoded';
  DataRequest1.Request.Params['SQLTEXT']:=EncodeString(Format('SELECT * FROM TMP_YS where zbwh=%S and zt in ( %S  )',[QuotedStr(''),QuotedStr('1')]));
  DataRequest1.Post();
end;

procedure TForm1.Btnx_4Click(Sender: TObject);
begin
  HttpClient1.ServerAddr:='192.168.0.51';
  HttpClient1.ServerPort:='8181';
  if HttpClient1.isConnecting then
  begin
    HttpClient1.Disconnect
  end;
  HttpClient1.Connect();

  DataRequest1.Request.Method  :='POST';
  DataRequest1.Request.FileName:=Format('/tuser.updatedb',[]);
  DataRequest1.Request.Host:=HttpClient1.ServerAddr;
  DataRequest1.Request.ContentType:='application/x-www-form-urlencoded;text/plain;charset=utf-8';
  DataRequest1.Request.Params['DATA']:='TMP_YS';
  DataRequest1.Post();
end;

procedure TForm1.HttpClient1ConnectError(Sender: TRtcConnection;
  E: Exception);
begin
  ShowMessageFmt('CONNECT EROR:%S',[E.Message]);
end;

procedure TForm1.HttpClient1ConnectFail(Sender: TRtcConnection);
begin
  ShowMessageFmt('CONNECT FAIL',[]);
end;

procedure TForm1.Chkb_1Click(Sender: TObject);
begin
  HttpServer1.ServerAddr:='192.168.0.51';
  HttpServer1.ServerPort:='8181';
  HttpServer1.Listen();
end;

procedure TForm1.RtcDataProvider1CheckRequest(Sender: TRtcConnection);
begin
  with TRtcDataServer(Sender) do
  begin
    Accept;
  end;
end;

procedure TForm1.RtcDataProvider1DataReceived(Sender: TRtcConnection);
begin
  with TRtcDataServer(Sender) do
  begin
    if Request.Complete then
    begin
      ShowMessageFmt('%S',[Read]);
    end;
  end;
end;

end.
